<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class admin_event extends CI_Controller {
    private $limit = 20;
    function __construct() {
        parent::__construct();
        $this->load->model('model_event', 'm_event', TRUE);
        $this->load->model('model_gallery', 'm_gall', TRUE);
    }
    
    function index($offset = 0, $order_column = 'datecreated', $order_type = 'ASC') {
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        if($this->session->userdata('admin_user')->level == 2){
            $data = array(
                'web_title' =>  'Admin | List All Event',
                'main_view' =>  'mimin/event/index_new',
                'list_data' =>  $this->m_event->get_all_events_by_stateid($this->session->userdata('admin_user')->state_id)
            );
        }
        else{
            $data = array(
                'web_title' =>  'Admin Event',
                'main_view' =>  'mimin/event/index_new',
                'list_data' =>  $this->m_event->get_all_events()
            );
        }
        
        $this->load->view('mimin/template/template_table',$data);
    }
    function add(){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }else{
            $condition  = " `type` = 'i'";
            $data       = array(
                'web_title'             =>  'Data Event Baru',
                'msg_error_username'    =>  form_error('judul','<caption>','</caption>'),
                'msg_error_name'        =>  form_error('content','<caption>','</caption>'),
                'main_view'             =>  "mimin/event/add_new",
                'list_data'             =>  $this->m_gall->get_data_by_condition($condition)
            );
            $this->parser->parse("mimin/template/template_editor",$data);
        }
    }
    
    function add_event(){
        $this->form_validation->set_rules('judul','Judul','required');
        $this->form_validation->set_rules('content','Isi Konten','required');
        
        if($this->form_validation->run() == FALSE){
            $condition  = " `type` = 'i'";
            $data       = array(
                'web_title'             =>  'Data Event Baru',
                'msg_error_username'    =>  form_error('judul','<caption>','</caption>'),
                'msg_error_name'        =>  form_error('content','<caption>','</caption>'),
                'main_view'             =>  "mimin/event/add_new",
                'list_data'             =>  $this->m_gall->get_data_by_condition($condition)
            );
            $this->parser->parse("mimin/template/template_editor",$data);
        }else{
            $curdate                    = date("Y-m-d h:i:s");
            $isi                        = array(
                'judul'                 =>  $this->input->post('judul'),
                'content'               =>  $this->input->post('content'),
                'eventdate'             =>  $this->input->post('tanggal_event'),
                'createdby'             =>  $this->session->userdata('admin_user')->id_admin,
                'datecreated'           =>  $curdate,
                'datemodified'          =>  $curdate,
                'place'                 =>  $this->input->post('place'),
                'state_id'              =>  $this->session->userdata('admin_user')->state_id,
            );
            
            if( $this->m_event->save($isi) ){
                $event_id               = $this->db->insert_id();
                $image_event            = ippi_isset($this->input->post('image_event'));
            
                if( !empty($image_event) ){
                    foreach($image_event as $id){
                        $data_link      = array(
                            'id_gallery'=> $id,
                            'id'        => $event_id,
                            'type'      => 'event',
                        );
                        $this->m_gall->save_data_link($data_link);
                    }
                }

                $condition              = " `type` = 'i'";
                $data                   = array(
                    "main_view"         =>  "mimin/event/add_new",
                    "web_title"         =>  "Successfull Add New Event Data",
                    "success_status"    =>  1,
                    'list_data'         =>  $this->m_gall->get_data_by_condition($condition)
                );
                $this->load->view("mimin/template/template_editor",$data);
            } 
        }
    }
    
    function delete($id=""){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($id==""){
            redirect("admin_event","refresh");
        }
        else{
            //JIKA koordinator Propinsi
            if($this->session->userdata('admin_user')->level == 2){
                //cek database 
                $list_event = $this->m_event->select_by_id($id);
                $stateid="";
                foreach ($list_event as $r){
                    $stateid = $r->state_id;
                }
                
                if($this->session->userdata('admin_user')->state_id == $stateid){
                    if($this->m_event->delete($id)){
                        redirect("admin_event","refresh");
                    }
                    else{
                        redirect("admin_event","refresh");
                    }
                }
                else{
                    redirect("admin_event","refresh");
                }
            }
            else{
                if($this->m_event->delete($id)){
                    redirect("admin_event","refresh");
                }
            }
        }
    }
    function detail($id=""){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($id==""){
            redirect("admin_event","refresh");
        }
        else{
            //SET TIME ZONE
            date_default_timezone_set("Asia/Jakarta");
            $curdate = date("Y-m-d h:i:s");
            
            $count = $this->m_event->select_by_id($id,"count");
     
            if($count > 0){
                $this->form_validation->set_rules('judul','Judul','required');
                $this->form_validation->set_rules('content','Isi Konten','required');
                if($this->form_validation->run() == FALSE){
                    $data = array(
                        "main_view"     =>  "mimin/event/detail",
                        "web_title"     =>  "Data Event Detail",
                        "list_data"     =>  $this->m_event->select_by_id($id)
                    );            
                    $this->load->view("mimin/template/template_editor",$data);
                }
                else{
                    $config['upload_path'] = './content/media/events/';
                    $config['allowed_types'] = 'jpg|jpeg|png';
                    $this->load->library('upload', $config);
                    
                    //IF THE ADMIN Do Not Choose any image to upload
                    if (!$this->upload->do_upload()){
                        //echo"sini".$this->upload->display_errors();exit();
                        $isi_update = array(
                            "judul"         =>  $this->input->post("judul"),
                            "place"         =>  $this->input->post("place"),
                            "content"       =>  $this->input->post("content"),
                            "datemodified"  =>  $curdate,
                            "modifiedby"    =>  $this->session->userdata('admin_user')->id_admin,
                            "eventdate"     =>  $this->input->post("tanggal_event"),
                        );
                        $status_update = $this->m_event->update($id,$isi_update);
                        if($status_update == false){
                            echo"false";
                        }
                        else{
                            $data = array(
                                "main_view"         =>  "mimin/event/detail",
                                "web_title"         =>  "Successfull Update Event Data",
                                "success_status"    =>  1,
                                "list_data"         =>  $this->m_event->select_by_id($id)
                            );
                            $this->load->view("mimin/template/template_editor",$data);
                        }
                        $data = array(
                            "main_view"     =>  "mimin/event/add_new",
                            "web_title"     =>  "Add New Event Data",
                            "error"         =>  $this->upload->display_errors()
                        );
                        //$this->load->view("mimin/template/template_editor",$data);
                    }
                    else{
                        //echo"sini";exit();
                        
                        $upload_image = $this->upload->data();
                        $isi_update = array(
                            'judul'         =>  $this->input->post('judul'),
                            'content'       =>  $this->input->post('content'),
                            'datemodified'  =>  $curdate,
                            'place'         =>  $this->input->post('place'),
                            "modifiedby"    =>  $this->session->userdata('admin_user')->id_admin,
                            'image'         =>  'content/media/events/'.$upload_image['file_name'],
                            "eventdate"     =>  $this->input->post("tanggal_event"),
                        );
                        
                        $status_update = $this->m_event->update($id,$isi_update);
                        if($status_update){
                            $data = array(
                                "main_view"         =>  "mimin/event/detail",
                                "web_title"         =>  "Successfull Add new Event Data",
                                "success_status"    =>  1,
                                "list_data"         =>  $this->m_event->select_by_id($id)
                            );
                            $this->load->view("mimin/template/template_editor",$data);
                        }
                        else{
                            echo"Cannot update data";
                        }
                    }
                }
            }
            else{
                redirect("admin_event","refresh");
            }
        }
    }
}

/* End of file admin_event.php */
/* Location: ./application/controllers/admin_event.php */